package cn.globalph.housekeeper.utils

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Build
import cn.globalph.housekeeper.utils.CoordinateTransformUtil.pi
import androidx.core.app.NotificationCompat
import cn.globalph.housekeeper.R
import cn.globalph.housekeeper.ui.main.MainActivity
import java.util.*


object NotificationUtil {

    fun notifyMessage(context: Context){
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        var notification:Notification? = null

        val intent = Intent(context,MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(context,1,intent,0)

        val id = "house_holder_1"
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val name = "service_time_over_notify"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel(id,name,importance)
            channel.enableLights(true)
            channel.lightColor = Color.GREEN
            channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            channel.setBypassDnd(true)
//            channel.enableVibration(true)
//            channel.vibrationPattern = longArrayOf(100,200,300,400,500,400,300,200,400)
            channel.description = "服务时间还有10分钟，请抓紧时间"
//            var file = File(Uri.parse("file:///android_asset/sound.mp3").path)
            channel.setSound(Uri.parse("android.resource://" + context.packageName + "/" +R.raw.sound),null)
//            channel.setSound(null,null)
            notificationManager.createNotificationChannel(channel)
        }

        val builder = NotificationCompat.Builder(context,id)
        builder.setContentTitle("提示")
            .setContentText("服务时间还有10分钟，请抓紧时间")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .setOngoing(false)
            .setWhen(System.currentTimeMillis())
//        if (obj.subText != null && obj.subText.trim().length() > 0) {
//            builder.setSubText(obj.subText)
//        }
        notification = builder.build()
        notification.defaults  = Notification.DEFAULT_ALL
        notificationManager.notify(Random().nextInt(1000),notification)
    }
}